
/*

___________________________________________________________________________
Income mobility analysis includes the following items

_________
Main text

Figure 4: Average income trajectories of incorporated entrepreneurs and wage earners

Figure 5: Differences in income trajectories between entrepreneurs and wage earners by individual and parental income

Figure 6: Entrepreneurship and income mobility

_________
Appendix

Figure A8: Income trajectories by firm survival five years after establishment

Figure A9: Composition of market income sources by individual income

Figure B1: Average market income trajectories when accounting for retained earnings

Figure B2: Differences in income trajectories between entrepreneurs and wage earners by individual and parental income: Market income

Figure B3: Differences in income trajectories between entrepreneurs and wage earners by individual and parental income: Market income with retained earnings

Table B1: Sensitivity of the gains from entrepreneurship to the estimation sample

Figure B4: Disposable income trajectories using a balanced panel of individuals

Figure B5: Gains in disposable income by industry of the new incorporated business

*/









/*
________________________________________________________________________________
Main text
*/

/*
________________________________________________________________________________
Figure 4: Average income trajectories of incorporated entrepreneurs and wage earners
*/

********************************************************************************
********************************************************************************
********************************************************************************

/*
Panels A and D first
- come from the same nested model, where
	- wage earners's trajectories are the base
	- entrepreneurship and parental entrepreneurship interactions are added
Panel B: OLS done here too, "new" DiD separately
*/

use "$data\01_main\folk_flown_merge_eventready.dta", clear

* residualize income with controls
rename sukup sex
rename ika_tmin1 age
gen agesq = age*age
gen agecub = agesq*age
rename highed_tmin1 edu
rename ptile_prnt parent
		gen edu_x_age=edu*age
		gen edu_x_agesq=edu*agesq
		gen edu_x_agecub=edu*agecub
		gen sex_x_age=sex*age
		gen sex_x_agesq=sex*agesq
		gen sex_x_agecub=sex*agecub
		gen edu_x_sex=edu*sex
foreach var of varlist log_dspi log_mkti log_mkti_adj1 {
reg `var' i.edu i.sex i.age i.edu_x_age i.edu_x_agesq i.edu_x_agecub i.sex_x_age i.sex_x_agesq i.sex_x_agecub i.edu_x_sex
predict res, res
sum `var', meanonly
replace res=res+r(mean)
rename res `var'_res
}

* define "treatment"
keep if event_ie!=. | event_we!=.
replace ie=0
replace ie=1 if event_ie!=. // IE labeled as IE already before becoming an entrepreneur
gen event=event_ie
replace event=event_we if event_ie==.

********************************************************************************

* Figure 4, Panel A: the base from here can be used for WE in all future specifications
	* --> IE interactions just added on top of it
	* Panel B with controls also collected here

preserve
	replace event=event+6
	gen nro=_n-6
	gen base=0
	gen base_se=0
	gen eff_ie=0
	gen eff_ie_se=0
	gen cov=0
	
	reg log_dspi_res ib5.event##i.ie i.vuosi i.vuosi#i.ie, vce(r)
		forvalues i = 1(1)16{
		qui{
			local c=`i'
			replace base=_b[`i'.event] in `c'
			replace base_se=_se[`i'.event] in `c'
			replace eff_ie=_b[`i'.event#1.ie] in `c'
			replace eff_ie_se=_se[`i'.event#1.ie] in `c'
			replace cov=e(V)["`i'.event", "`i'.event#1.ie"] in `c'
			}
			}
	
	keep nro-eff_ie_se cov
	keep if nro < 11
	
	* adding
	gen lvl_ie = base + eff_ie
	gen lvl_ie_se = base_se*base_se + eff_ie_se*eff_ie_se
	replace lvl_ie_se = sqrt(lvl_ie_se + 2*cov)
	order nro base base_se lvl_ie lvl_ie_se eff_ie eff_ie_se
	keep nro base base_se lvl_ie lvl_ie_se eff_ie eff_ie_se
	
	label var base "Figure 4, Panel A, Wage earners"
	label var lvl_ie "Figure 4, Panel A, Entrepreneurs"
	label var eff_ie "Figure 4, Panel B, OLS with controls"
	save "$results\04_incomemobility\fig4.dta", replace
restore

********************************************************************************

* Figure 4, Panel B, without controls

preserve
	replace event=event+6
	gen nro=_n-6
	gen base=0
	gen base_se=0
	gen eff_ie=0
	gen eff_ie_se=0
	gen cov=0
	
	reg log_dspi ib5.event##i.ie i.vuosi i.vuosi#i.ie, vce(r)
		forvalues i = 1(1)16{
		qui{
			local c=`i'
			replace base=_b[`i'.event] in `c'
			replace base_se=_se[`i'.event] in `c'
			replace eff_ie=_b[`i'.event#1.ie] in `c'
			replace eff_ie_se=_se[`i'.event#1.ie] in `c'
			replace cov=e(V)["`i'.event", "`i'.event#1.ie"] in `c'
			}
			}
	
	keep nro-eff_ie_se cov
	keep if nro < 11
	
	rename eff_ie eff_ie_noc
	rename eff_ie_se eff_ie_se_noc
	keep nro eff_ie_noc eff_ie_se_noc
	
	merge 1:1 nro using "$results\04_incomemobility\fig4.dta"
	drop _merge
	order eff_ie_noc eff_ie_se_noc, last
	label var eff_ie_noc "Figure 4, Panel B, OLS without controls"
	save "$results\04_incomemobility\fig4.dta", replace
restore

********************************************************************************

* Figure 4, Panel D: need to add interaction with parental entrepreneurship

preserve
	drop if ie==1 & prnt_entr==0
	replace event=event+6
	gen nro=_n-6
	gen base=0
	gen base_se=0
	gen eff_ie=0
	gen eff_ie_se=0
	gen cov=0
	
	reg log_dspi_res ib5.event##i.ie i.vuosi i.vuosi#i.ie, vce(r)
		forvalues i = 1(1)16{
		qui{
			local c=`i'
			replace base=_b[`i'.event] in `c'
			replace base_se=_se[`i'.event] in `c'
			replace eff_ie=_b[`i'.event#1.ie] in `c'
			replace eff_ie_se=_se[`i'.event#1.ie] in `c'
			replace cov=e(V)["`i'.event", "`i'.event#1.ie"] in `c'
			}
			}
	
	keep nro-eff_ie_se cov
	rename eff_ie eff_ie_peyes
	rename eff_ie_se eff_ie_peyes_se
	keep if nro < 11
	
	* adding
	gen lvl_ie_peyes = base + eff_ie_peyes
	gen lvl_ie_peyes_se = base_se*base_se + eff_ie_peyes_se*eff_ie_peyes_se
	replace lvl_ie_peyes_se = sqrt(lvl_ie_peyes_se + 2*cov)
	keep nro lvl_ie_peyes lvl_ie_peyes_se
	
	merge 1:1 nro using "$results\04_incomemobility\fig4.dta"
	drop _merge
	order lvl_ie_peyes lvl_ie_peyes_se, last
	label var lvl_ie_peyes "Figure 4, Panel D, yes"
	save "$results\04_incomemobility\fig4.dta", replace
restore

preserve
	drop if ie==1 & prnt_entr==1
	replace event=event+6
	gen nro=_n-6
	gen base=0
	gen base_se=0
	gen eff_ie=0
	gen eff_ie_se=0
	gen cov=0
	
	reg log_dspi_res ib5.event##i.ie i.vuosi i.vuosi#i.ie, vce(r)
		forvalues i = 1(1)16{
		qui{
			local c=`i'
			replace base=_b[`i'.event] in `c'
			replace base_se=_se[`i'.event] in `c'
			replace eff_ie=_b[`i'.event#1.ie] in `c'
			replace eff_ie_se=_se[`i'.event#1.ie] in `c'
			replace cov=e(V)["`i'.event", "`i'.event#1.ie"] in `c'
			}
			}
	
	keep nro-eff_ie_se cov
	rename eff_ie eff_ie_peno
	rename eff_ie_se eff_ie_peno_se
	keep if nro < 11
	
	* adding
	gen lvl_ie_peno = base + eff_ie_peno
	gen lvl_ie_peno_se = base_se*base_se + eff_ie_peno_se*eff_ie_peno_se
	replace lvl_ie_peno_se = sqrt(lvl_ie_peno_se + 2*cov)
	keep nro lvl_ie_peno lvl_ie_peno_se
	
	merge 1:1 nro using "$results\04_incomemobility\fig4.dta"
	drop _merge
	order lvl_ie_peno lvl_ie_peno_se, last
	label var lvl_ie_peno "Figure 4, Panel D, no"
	save "$results\04_incomemobility\fig4.dta", replace
restore

********************************************************************************
********************************************************************************
********************************************************************************

/*
Panel B: Callaway & Sant'Anna and Borusyak et al.
*/

* define the cohort for csdid
gen first_treat0=0
replace first_treat0=vuosi if new_ie==1
bysort id: egen first_treat=max(first_treat0)
rename ie gr

* keep only the necessary variables
keep id vuosi log_dspi log_dspi_res event gr first_treat

********************************************************************************

/*
Accounting for different time effects for the two groups is not supported in the
Stata routines of Callaway & Sant'Anna and Borusyak et al.
--> see how the results look in basic DiD (OLS) to get a scaling function (approximation)

In OLS, this is achieved by including i.vuosi#i.gr
*/

preserve // with i.vuosi#i.gr
replace event=event+6
gen nro=_n-6
gen eff=0
gen eff_up=0
gen eff_lo=0
reg log_dspi_res ib5.event##i.gr i.vuosi i.vuosi#i.gr, vce(r)
	forvalues i = 1(1)16{
	qui{
		local c=`i'
		replace eff=_b[`i'.event#1.gr] in `c'
		replace eff_up=_b[`i'.event#1.gr] + 1.96*_se[`i'.event#1.gr] in `c'
		replace eff_lo=_b[`i'.event#1.gr] - 1.96*_se[`i'.event#1.gr] in `c'
		}
		}
keep nro eff
rename eff eff_w
keep if nro < 11
list
	save "$results\04_incomemobility\temp_with.dta", replace
restore
preserve // without i.vuosi#i.gr
replace event=event+6
gen nro=_n-6
gen eff=0
gen eff_up=0
gen eff_lo=0
reg log_dspi_res ib5.event##i.gr i.vuosi, vce(r)
	forvalues i = 1(1)16{
	qui{
		local c=`i'
		replace eff=_b[`i'.event#1.gr] in `c'
		replace eff_up=_b[`i'.event#1.gr] + 1.96*_se[`i'.event#1.gr] in `c'
		replace eff_lo=_b[`i'.event#1.gr] - 1.96*_se[`i'.event#1.gr] in `c'
		}
		}
keep nro eff
rename eff eff_wo
keep if nro < 11
list
	save "$results\04_incomemobility\temp_without.dta", replace
restore

preserve
use "$results\04_incomemobility\temp_with.dta", clear
merge 1:1 nro using "$results\04_incomemobility\temp_without.dta"
drop _merge
gen scalar=eff_w-eff_wo
list
save "$results\04_incomemobility\timetrend_scalar.dta", replace
restore

erase "$results\04_incomemobility\temp_with.dta"
erase "$results\04_incomemobility\temp_without.dta"

********************************************************************************

/*
Estimation with did_imputation of Borusyak et al.
*/

* Estimation
preserve
replace first_treat=. if first_treat==0
did_imputation log_dspi_res id vuosi first_treat, allhorizons pretrend(5)
restore

* Save the results
matrix b=e(b)
preserve
gen nro=_n-6
gen b_bjs=.
gen se_bjs=.
* Pre-years
replace b_bjs=b[1,12] if nro==-5
replace b_bjs=b[1,13] if nro==-4
replace b_bjs=b[1,14] if nro==-3
replace b_bjs=b[1,15] if nro==-2
replace b_bjs=0 if nro==-1
replace se_bjs=_se[pre1] if nro==-5
replace se_bjs=_se[pre2] if nro==-4
replace se_bjs=_se[pre3] if nro==-3
replace se_bjs=_se[pre4] if nro==-2
replace se_bjs=0 if nro==-1
* Post-years
forval i=1(1)11{
	replace b_bjs=b[1,`i'] if nro==`i'-1
}
forval i=0(1)10{
	replace se_bjs=_se[tau`i'] if nro==`i'
}

keep nro b_bjs se_bjs
keep if nro<11
merge 1:1 nro using "$results\04_incomemobility\timetrend_scalar.dta"
drop _merge
replace b_bjs=b_bjs+scalar
keep nro b_bjs se_bjs
save "$results\04_incomemobility\bjs.dta", replace
restore

********************************************************************************

/*
Estimation with csdid of Callaway and Sant'Anna
*/

* Estimation
csdid log_dspi_res, ivar(id) time(vuosi) gvar(first_treat) method(dripw) wboot reps(5)
estat event, window(-5 10) estore(did_cs_dripw)

* Save the results: done manually because collecting the results from 'csdid' is not well-documented (one can plot event study figures easily, but not collect estimates)
gen nro=_n-6
gen b_cs=.
gen se_cs=.
replace b_cs=0.0357 if nro==-5
replace b_cs=0.0357 if nro==-4
replace b_cs=0.0224 if nro==-3
replace b_cs=0.0288 if nro==-2
replace b_cs=-0.0228 if nro==-1
replace b_cs=-0.0931 if nro==0
replace b_cs=-0.0617 if nro==1
replace b_cs=-0.0271 if nro==2
replace b_cs=-0.0057 if nro==3
replace b_cs=0.0110 if nro==4
replace b_cs=0.0284 if nro==5
replace b_cs=0.0284 if nro==6
replace b_cs=0.0290 if nro==7
replace b_cs=0.0203 if nro==8
replace b_cs=0.0285 if nro==9
replace b_cs=0.0158 if nro==10
replace se_cs=0.0031 if nro==-5
replace se_cs=0.0031 if nro==-4
replace se_cs=0.0031 if nro==-3
replace se_cs=0.0031 if nro==-2
replace se_cs=0.0033 if nro==-1
replace se_cs=0.0042 if nro==0
replace se_cs=0.0048 if nro==1
replace se_cs=0.0048 if nro==2
replace se_cs=0.0051 if nro==3
replace se_cs=0.0053 if nro==4
replace se_cs=0.0054 if nro==5
replace se_cs=0.0057 if nro==6
replace se_cs=0.0061 if nro==7
replace se_cs=0.0071 if nro==8
replace se_cs=0.0076 if nro==9
replace se_cs=0.0084 if nro==10
keep nro b_cs se_cs
keep if nro<11
merge 1:1 nro using "$results\04_incomemobility\timetrend_scalar.dta"
drop _merge
replace b_cs=b_cs+scalar
keep nro b_cs se_cs
save "$results\04_incomemobility\cs.dta", replace

********************************************************************************

/*
Merge with the main results file
*/

use "$results\04_incomemobility\fig4.dta", clear
merge 1:1 nro using "$results\04_incomemobility\cs.dta"
drop _merge
merge 1:1 nro using "$results\04_incomemobility\bjs.dta"
drop _merge
order b_cs se_cs b_bjs se_bjs lvl_ie_peyes lvl_ie_peyes_se lvl_ie_peno lvl_ie_peno_se, last
label var b_cs "Figure 4, Panel B, CS"
label var b_bjs "Figure 4, Panel B, BJS"
erase "$results\04_incomemobility\timetrend_scalar.dta"
erase "$results\04_incomemobility\cs.dta"
erase "$results\04_incomemobility\bjs.dta"
save "$results\04_incomemobility\fig4.dta", replace

********************************************************************************
********************************************************************************
********************************************************************************

/*
Panel C: Coarsened exact matching
*/

use "$data\01_main\folk_flown_merge_eventready.dta", clear
keep if event_ie!=. | event_we!=.
keep shnro-dspi log_dspi ptile_dspi ika shr_linc event_ie event_we sukup pks highed married lkm_k ptile_prnt prnt_entr
gen event=.
replace event=event_ie
replace event=event_we if event==.
drop ie
gen ie=0
replace ie=1 if event_ie!=.
drop event_ie event_we

* CEM
preserve
xtset id vuosi, yearly
gen d_ptile_dspi=ptile_dspi-L2.ptile_dspi
foreach var of varlist ptile_dspi shr_linc {
	replace `var' = (`var'+L.`var'+L2.`var')/3 if event==-1
}
keep if event==-1
order shnro-ptile_dspi d_ptile_dspi ptile_prnt shr_linc
keep shnro vuosi ptile_dspi d_ptile_dspi ptile_prnt shr_linc ika sukup pks highed married lkm_k ie prnt_entr
replace highed=3 if highed==99
replace lkm_k=3 if lkm_k>3
drop if d_ptile_dspi==. | ptile_dspi==. | shr_linc==.
cem vuosi (#0) ptile_dspi (#10) d_ptile_dspi (#10) ptile_prnt (#10) shr_linc (#5) ika (25.5 36.5 47.5 58.5) sukup (#0) pks (#0) lkm_k (#0) prnt_entr (#0), tr(ie)
keep shnro cem_weights
save "$results\04_incomemobility\weights.dta", replace
restore
keep shnro id vuosi log_dspi event ie
merge m:1 shnro using "$results\04_incomemobility\weights.dta"
keep if _merge==3
drop _merge

* Event study regressions
preserve
keep if ie==1
gen nro=_n-6
gen eff=0
gen eff_up=0
gen eff_lo=0
char event[omit] -1
qui xi: reg log_dspi i.event i.vuosi [iweight=cem_weights], vce(r)
	forvalues i = 1(1)4{
	qui{
		local c=`i'
		replace eff=_b[_Ievent_`i'] in `c'
		replace eff_up=_b[_Ievent_`i'] + 1.96*_se[_Ievent_`i'] in `c'
		replace eff_lo=_b[_Ievent_`i'] - 1.96*_se[_Ievent_`i'] in `c'
		}
		}
	forvalues i = 6(1)16{
	qui{
		local c=`i'
		replace eff=_b[_Ievent_`i'] in `c'
		replace eff_up=_b[_Ievent_`i'] + 1.96*_se[_Ievent_`i'] in `c'
		replace eff_lo=_b[_Ievent_`i'] - 1.96*_se[_Ievent_`i'] in `c'
		}
		}
keep if nro<=10
keep nro-eff_lo
save "$results\04_incomemobility\cem_iewe_dspi_ie", replace
restore
preserve
keep if ie==0
gen nro=_n-6
gen eff=0
gen eff_up=0
gen eff_lo=0
char event[omit] -1
qui xi: reg log_dspi i.event i.vuosi [iweight=cem_weights], vce(r)
	forvalues i = 1(1)4{
	qui{
		local c=`i'
		replace eff=_b[_Ievent_`i'] in `c'
		replace eff_up=_b[_Ievent_`i'] + 1.96*_se[_Ievent_`i'] in `c'
		replace eff_lo=_b[_Ievent_`i'] - 1.96*_se[_Ievent_`i'] in `c'
		}
		}
	forvalues i = 6(1)16{
	qui{
		local c=`i'
		replace eff=_b[_Ievent_`i'] in `c'
		replace eff_up=_b[_Ievent_`i'] + 1.96*_se[_Ievent_`i'] in `c'
		replace eff_lo=_b[_Ievent_`i'] - 1.96*_se[_Ievent_`i'] in `c'
		}
		}
keep if nro<=10
keep nro-eff_lo
save "$results\04_incomemobility\cem_iewe_dspi_we", replace
restore
* erase "$results\04_incomemobility\weights.dta"

* Merge with the main results file

use "$results\04_incomemobility\fig4.dta", clear
merge 1:1 nro using "$results\04_incomemobility\cem_iewe_dspi_we.dta"
drop _merge
rename eff cem_base
rename eff_up cem_base_up
rename eff_lo cem_base_lo
merge 1:1 nro using "$results\04_incomemobility\cem_iewe_dspi_ie.dta"
drop _merge
rename eff cem_lvl_ie
rename eff_up cem_lvl_ie_up
rename eff_lo cem_lvl_ie_lo
order cem_base cem_base_up cem_base_lo cem_lvl_ie cem_lvl_ie_up cem_lvl_ie_lo lvl_ie_peyes lvl_ie_peyes_se lvl_ie_peno lvl_ie_peno_se, last

label var cem_base "Figure 4, Panel C, Wage earners"
label var cem_lvl_ie "Figure 4, Panel C, Entrepreneurs"

* Standard errors needed only for Panel B --> drop the redundant variables
drop base_se lvl_ie_se cem_base_up cem_base_lo cem_lvl_ie_up cem_lvl_ie_lo lvl_ie_peyes_se lvl_ie_peno_se
save "$results\04_incomemobility\fig4.dta", replace
erase "$results\04_incomemobility\weights.dta"
erase "$results\04_incomemobility\cem_iewe_dspi_ie.dta"
erase "$results\04_incomemobility\cem_iewe_dspi_we.dta"

/*
________________________________________________________________________________
Figure 5: Differences in income trajectories between entrepreneurs and wage earners by individual and parental income
*/

use "$data\01_main\folk_flown_merge_eventready.dta", clear
keep shnro id vuosi log_dspi event_ie-ika_tmin1 highed_tmin1 ptile_dspi_tmin1 ptile_prnt
rename sukup sex
rename ika_tmin1 age
gen agesq = age*age
gen agecub = agesq*age
rename highed_tmin1 edu

* Create interactions manually as 'xi: reghdfe' does not work with '#' or '##'
		gen edu_x_age=edu*age
		gen edu_x_agesq=edu*agesq
		gen edu_x_agecub=edu*agecub
		gen sex_x_age=sex*age
		gen sex_x_agesq=sex*agesq
		gen sex_x_agecub=sex*agecub
		gen edu_x_sex=edu*sex

* Quintiles		
egen quint_dspi_own = cut(ptile_dspi_tmin1), at(1(20)101)
egen quint_dspi_prnt = cut(ptile_prnt), at(1(20)101)

* Fails with IE interaction --> estimate IE and WE separately	
	
/*
								IE
*/

preserve


	keep if event_ie!=.

	egen long CL = group(quint_dspi_own quint_dspi_prnt) // 25 groups
	gen y=log_dspi
	rename event_ie event

		*
		gen event_time=_n-6		
		forval i=1/25 {
		gen b`i'=0
		gen se`i'=0
		}
		
		*char event[omit] -1
		gen eventt=event+6
		levelsof CL, local(list)
		levelsof eventt, local(eve)
		foreach c in `list'{
			foreach x in `eve'{
			gen interact_`c'_`x'=(CL==`c')*(eventt==`x')
			}
		}
		tab event, gen(eve)
		tab CL, gen(cl)
		forval i=1/25{
			drop interact_`i'_5
		}
		drop eve5 cl1
		drop interact_1_*

		reg y eve1-eve16 cl* interact_* i.vuosi i.edu i.sex i.age i.edu_x_age i.edu_x_agesq i.edu_x_agecub i.sex_x_age i.sex_x_agesq i.sex_x_agecub i.edu_x_sex, vce(r)
			replace b1=_b[_cons] if event_time==-1
			replace se1=_se[_cons] if event_time==-1
			forval i = 2/25 {
			replace b`i'=_b[_cons]+_b[cl`i'] if event_time==-1
			replace se`i'=_se[cl`i'] if event_time==-1
			}
		forval j = 1/4 {
			replace b1=_b[_cons]+_b[eve`j'] if _n==`j'
			replace se1=_se[eve`j'] if _n==`j'
			}
		forval j = 6/16 {
			replace b1=_b[_cons]+_b[eve`j'] if _n==`j'
			replace se1=_se[eve`j'] if _n==`j'
			}	
		forval j=1/4 {
		forval i = 2/25 {
			replace b`i'=_b[_cons]+_b[cl`i']+_b[eve`j']+_b[interact_`i'_`j'] if _n==`j'
			replace se`i'=_se[interact_`i'_`j'] if _n==`j'
			}
		}
		forval j=6/16 {
		forval i = 2/25 {
			replace b`i'=_b[_cons]+_b[cl`i']+_b[eve`j']+_b[interact_`i'_`j'] if _n==`j'
			replace se`i'=_se[interact_`i'_`j'] if _n==`j'
			}
		}

	keep if event_time<=10
	keep event_time - se25
	foreach var of varlist b1 - se25 {
		rename `var' `var'_ie
	}
	
	save "$results\04_incomemobility\ie.dta", replace

restore

/*
							WE
*/

preserve

	keep if event_we!=.

	egen long CL = group(quint_dspi_own quint_dspi_prnt) // 25 groups
	gen y=log_dspi
	rename event_we event
	* 50% sample only: uncomment if want to restrict sample size
	/*gen random=runiform() if event==-1
	gen keep=1 if random<=0.5
	bys id: egen min_keep=min(keep)
	keep if min_keep==1*/
	
		*
		gen event_time=_n-6		
		forval i=1/25 {
		gen b`i'=0
		gen se`i'=0
		}
		
		*char event[omit] -1
		gen eventt=event+6
		levelsof CL, local(list)
		levelsof eventt, local(eve)
		foreach c in `list'{
			foreach x in `eve'{
			gen interact_`c'_`x'=(CL==`c')*(eventt==`x')
			}
		}
		tab event, gen(eve)
		tab CL, gen(cl)
		forval i=1/25{
			drop interact_`i'_5
		}
		drop eve5 cl1 
		drop interact_1_*

		*di "$S_TIME $_DATE"
		*reghdfe y eventt##CL, vce(r) absorb(vuosi)
		*di "$S_TIME $_DATE"	
		di "$S_TIME $_DATE"
		reg y eve1-eve16 cl* interact_* i.vuosi i.edu i.sex i.age i.edu_x_age i.edu_x_agesq i.edu_x_agecub i.sex_x_age i.sex_x_agesq i.sex_x_agecub i.edu_x_sex, vce(r)
		di "$S_TIME $_DATE"
		
			replace b1=_b[_cons] if event_time==-1
			replace se1=_se[_cons] if event_time==-1
			forval i = 2/25 {
			replace b`i'=_b[_cons]+_b[cl`i'] if event_time==-1
			replace se`i'=_se[cl`i'] if event_time==-1
			}
		forval j = 1/4 {
			replace b1=_b[_cons]+_b[eve`j'] if _n==`j'
			replace se1=_se[eve`j'] if _n==`j'
			}
		forval j = 6/16 {
			replace b1=_b[_cons]+_b[eve`j'] if _n==`j'
			replace se1=_se[eve`j'] if _n==`j'
			}	
		forval j=1/4 {
		forval i = 2/25 {
			replace b`i'=_b[_cons]+_b[cl`i']+_b[eve`j']+_b[interact_`i'_`j'] if _n==`j'
			replace se`i'=_se[interact_`i'_`j'] if _n==`j'
			}
		}
		forval j=6/16 {
		forval i = 2/25 {
			replace b`i'=_b[_cons]+_b[cl`i']+_b[eve`j']+_b[interact_`i'_`j'] if _n==`j'
			replace se`i'=_se[interact_`i'_`j'] if _n==`j'
			}
		}

	keep if event_time<=10
	keep event_time - se25
	foreach var of varlist b1 - se25 {
		rename `var' `var'_we
	}
	
	save "$results\04_incomemobility\we.dta", replace

restore

/*
							Combine
*/

	use "$results\04_incomemobility\ie.dta", clear
	merge 1:1 event_time using "$results\04_incomemobility\we.dta"
	drop _merge
	rename event_time event
	save "$results\04_incomemobility\fig5.dta", replace

	erase "$results\04_incomemobility\ie.dta"
	erase "$results\04_incomemobility\we.dta"

/*
________________________________________________________________________________
Figure 6: Entrepreneurship and income mobility
*/

********************************************************************************
********************************************************************************
********************************************************************************

/*
Panel A: Income ranks of incorporated entrepreneurs
*/

* t-1, but need to be observed at t+10, too
use shnro ptile_dspi event_ie if event_ie==-1 | event_ie==10 using "$data\01_main\folk_flown_merge_eventready.dta", clear
bysort shnro: gen nobs = _N
keep if nobs==2
keep if event_ie==-1
bysort ptile_dspi: gen tm1 = _N
collapse tm1, by(ptile_dspi)
rename ptile_dspi rank
save "$results\04_incomemobility\tm1.dta", replace

* t+10, but need to be observed at t-1, too
use shnro ptile_dspi event_ie if event_ie==-1 | event_ie==10 using "$data\01_main\folk_flown_merge_eventready.dta", clear
bysort shnro: gen nobs = _N
keep if nobs==2
keep if event_ie==10
bysort ptile_dspi: gen tp10 = _N
collapse tp10, by(ptile_dspi)
rename ptile_dspi rank
save "$results\04_incomemobility\tp10.dta", replace

* all 2006-2016, "steady state"
use shnro vuosi ie dspi ika if inrange(vuosi,2006,2016) & ika>15 using "$data\01_main\folk_flown_merge.dta", clear
forvalues i = 2006(1)2016{
	fastxtile ptile = dspi if vuosi==`i' /*& ika>=25 & ika<=65*/, nq(100)
	rename ptile ptile_`i'
}
gen rank=.
forvalues i = 2006(1)2016{
	replace rank=ptile_`i' if vuosi==`i' //& ika>=25 & ika<=65
}
drop ptile_2006-ptile_2016
keep if ie==1
bysort rank: gen all = _N
collapse all, by(rank)

merge 1:1 rank using "$results\04_incomemobility\tm1.dta"
drop _merge
merge 1:1 rank using "$results\04_incomemobility\tp10.dta"
drop _merge
erase "$results\04_incomemobility\tm1.dta"
erase "$results\04_incomemobility\tp10.dta"

foreach x in all tm1 tp10 {
	egen `x'_ttl = sum(`x')
	gen `x'_shr = (`x' / `x'_ttl) * 100
}
drop *ttl

foreach x in all_shr tm1_shr tp10_shr {
	egen `x'_b50mean = mean(`x') if rank<=50
	replace `x' = `x'_b50mean if rank<=50
}
foreach x in all tm1 tp10 {
	egen `x'_b50n = sum(`x') if rank<=50
	replace `x' = `x'_b50n if rank<=50
}
drop all_shr_b50mean - tp10_b50n
drop if rank<50

label var all "Number of all incorporated entrepreneurs by income rank"
label var tm1 "Number of new incorporated entrepreneurs by income rank at t-1"
label var tp10 "Number of new incorporated entrepreneurs by income rank at t+10"
label var all_shr "Share of all incorporated entrepreneurs by income rank"
label var tm1_shr "Share of new incorporated entrepreneurs by income rank at t-1"
label var tp10_shr "Share of new incorporated entrepreneurs by income rank at t+10"
label var rank "Income rank, either full population or t-1 / t+10 event timeline"

save fig6_A, replace

********************************************************************************
********************************************************************************
********************************************************************************

/*
Panels B and C: Pr(Top) by own t-1 rank
*/

cd "$results\04_incomemobility"
use "$data\01_main\folk_flown_merge_eventready.dta", clear
keep if event_ie==-1 | event_ie==10 | event_we==-1 | event_we==10
keep shnro ptile_dspi event_ie event_we ptile_dspi_tmin1 sukup ika_tmin1 highed_tmin1 ptile_prnt married pks lkm_k prnt_entr
bysort shnro: gen nobs = _N
keep if nobs==2
drop nobs
keep if event_ie==10 | event_we==10
gen ie=0
replace ie=1 if event_ie==10
keep shnro ptile_dspi ptile_dspi_tmin1 ie sukup ika_tmin1 highed_tmin1 ptile_prnt married pks lkm_k prnt_entr
rename ptile_dspi rank_plus10
rename ptile_dspi_tmin1 rank_min1
gen top1_plus10=0
replace top1_plus10=1 if rank_plus10==100
gen top10_plus10=0
replace top10_plus10=1 if rank_plus10>90
gen bottom50_plus10=0
replace bottom50_plus10=1 if rank_plus10<=50

* Pool the bins into ventiles
egen vile = cut(rank_min1), at(1(5)101)
replace vile = vile + 1.5
drop rank_min1
rename vile rank_min1

gen gr=0 // wage earners
replace gr=1 if ie==1 // incorporated entrepreneurs
rename ika_tmin1 age // some controls
gen agesq = age*age
gen agecub = agesq*age
rename highed_tmin1 edu

cd "$results\04_incomemobility"
binsreg top1_plus10 rank_min1 age agesq agecub edu married lkm_k pks ptile_prnt prnt_entr if gr==1, ci(T) cb(T) nbins(20) savedata(b_ie) replace
binsreg top1_plus10 rank_min1 age agesq agecub edu married lkm_k pks ptile_prnt prnt_entr if gr==0, ci(T) cb(T) nbins(20) savedata(b_we) replace
binsreg top10_plus10 rank_min1 age agesq agecub edu married lkm_k pks ptile_prnt prnt_entr if gr==1, ci(T) cb(T) nbins(20) savedata(c_ie) replace
binsreg top10_plus10 rank_min1 age agesq agecub edu married lkm_k pks ptile_prnt prnt_entr if gr==0, ci(T) cb(T) nbins(20) savedata(c_we) replace
gr drop _all

use dots_x dots_fit CI_l CI_r using b_ie, clear
rename dots_fit top1_ie
rename CI_l top1_ie_95lo
rename CI_r top1_ie_95up
merge 1:1 dots_x using b_we
drop _merge
rename dots_fit top1_we
rename CI_l top1_we_95lo
rename CI_r top1_we_95up
merge 1:1 dots_x using c_ie
drop _merge
rename dots_fit top10_ie
rename CI_l top10_ie_95lo
rename CI_r top10_ie_95up
merge 1:1 dots_x using c_we
drop _merge
rename dots_fit top10_we
rename CI_l top10_we_95lo
rename CI_r top10_we_95up
gen vtile=_n
order vtile
keep vtile top*

erase b_ie.dta
erase b_we.dta
erase c_ie.dta
erase c_we.dta

label var vtile "Individual ventile rank at t-1"
label var top1_ie "Pr(Top 1%) entrepreneurs"
label var top1_we "Pr(Top 1%) wage earners"
label var top10_ie "Pr(Top 10%) entrepreneurs"
label var top10_we "Pr(Top 10%) wage earners"

save fig6_BC, replace

********************************************************************************
********************************************************************************
********************************************************************************

/*
Panels D and E: Pr(Top) by parental rank
*/

cd "$results\04_incomemobility"
use "$data\01_main\folk_flown_merge_eventready.dta", clear
keep if event_ie==-1 | event_ie==10 | event_we==-1 | event_we==10
keep shnro ptile_dspi event_ie event_we ptile_dspi_tmin1 sukup ika_tmin1 highed_tmin1 ptile_prnt married pks lkm_k prnt_entr
bysort shnro: gen nobs = _N
keep if nobs==2
drop nobs
keep if event_ie==10 | event_we==10
gen ie=0
replace ie=1 if event_ie==10
keep shnro ptile_dspi ptile_dspi_tmin1 ie sukup ika_tmin1 highed_tmin1 ptile_prnt married pks lkm_k prnt_entr
rename ptile_dspi rank_plus10
rename ptile_dspi_tmin1 rank_min1
gen top1_plus10=0
replace top1_plus10=1 if rank_plus10==100
gen top10_plus10=0
replace top10_plus10=1 if rank_plus10>90
gen bottom50_plus10=0
replace bottom50_plus10=1 if rank_plus10<=50
gen top1_min1=0
replace top1_min1=1 if rank_min1==100
gen top10_min1=0
replace top10_min1=1 if rank_min1>90
gen bottom50_min1=0
replace bottom50_min1=1 if rank_min1<=50

* Pool the bins into ventiles
egen vile = cut(ptile_prnt), at(1(5)101)
replace vile = vile + 1.5
rename vile rank_prnt

* Some renaming
rename ika_tmin1 age
gen agesq = age*age
gen agecub = agesq*age
rename highed_tmin1 edu

gen gr=0 // wage earners
replace gr=1 if ie==1 // incorporated entrepreneurs

cd "$results\04_incomemobility"
binsreg top1_plus10 rank_prnt age agesq agecub edu married lkm_k pks rank_min1 prnt_entr if gr==1, ci(T) cb(T) nbins(20) savedata(d_ie) replace
binsreg top1_plus10 rank_prnt age agesq agecub edu married lkm_k pks rank_min1 prnt_entr if gr==0, ci(T) cb(T) nbins(20) savedata(d_we) replace
binsreg top10_plus10 rank_prnt age agesq agecub edu married lkm_k pks rank_min1 prnt_entr if gr==1, ci(T) cb(T) nbins(20) savedata(e_ie) replace
binsreg top10_plus10 rank_prnt age agesq agecub edu married lkm_k pks rank_min1 prnt_entr if gr==0, ci(T) cb(T) nbins(20) savedata(e_we) replace
gr drop _all

use dots_x dots_fit CI_l CI_r using d_ie, clear
rename dots_fit top1_ie
rename CI_l top1_ie_95lo
rename CI_r top1_ie_95up
merge 1:1 dots_x using d_we
drop _merge
rename dots_fit top1_we
rename CI_l top1_we_95lo
rename CI_r top1_we_95up
merge 1:1 dots_x using e_ie
drop _merge
rename dots_fit top10_ie
rename CI_l top10_ie_95lo
rename CI_r top10_ie_95up
merge 1:1 dots_x using e_we
drop _merge
rename dots_fit top10_we
rename CI_l top10_we_95lo
rename CI_r top10_we_95up
gen vtile=_n
order vtile
keep vtile top*

erase d_ie.dta
erase d_we.dta
erase e_ie.dta
erase e_we.dta

label var vtile "Parental ventile rank"
label var top1_ie "Pr(Top 1%) entrepreneurs"
label var top1_we "Pr(Top 1%) wage earners"
label var top10_ie "Pr(Top 10%) entrepreneurs"
label var top10_we "Pr(Top 10%) wage earners"

save fig6_DE, replace









/*
________________________________________________________________________________
Appendix
*/

/*
________________________________________________________________________________
Figure A8: Income trajectories by firm survival five years after establishment
*/

* We can use Figure 4, Panel A to get wage earners

use "$data\01_main\folk_flown_merge_eventready.dta", clear

* residualize income with controls
rename sukup sex
rename ika_tmin1 age
gen agesq = age*age
gen agecub = agesq*age
rename highed_tmin1 edu
rename ptile_prnt parent
		gen edu_x_age=edu*age
		gen edu_x_agesq=edu*agesq
		gen edu_x_agecub=edu*agecub
		gen sex_x_age=sex*age
		gen sex_x_agesq=sex*agesq
		gen sex_x_agecub=sex*agecub
		gen edu_x_sex=edu*sex
foreach var of varlist log_dspi log_mkti log_mkti_adj1 {
reg `var' i.edu i.sex i.age i.edu_x_age i.edu_x_agesq i.edu_x_agecub i.sex_x_age i.sex_x_agesq i.sex_x_agecub i.edu_x_sex
predict res, res
sum `var', meanonly
replace res=res+r(mean)
rename res `var'_res
}

* define "treatment"
keep if event_ie!=. | event_we!=.
replace ie=0
replace ie=1 if event_ie!=. // IE labeled as IE already before becoming an entrepreneur
gen event=event_ie
replace event=event_we if event_ie==.

* merge dummy for survival
merge 1:1 shnro vuosi using "$data\01_main\folk_flown_merge_eventready_iespec.dta", keepusing(surv5yr) keep(1 3)
drop _merge
bysort shnro: egen surv5yr_allyears = max(surv5yr)
drop surv5yr
rename surv5yr_allyears surv5yr

preserve
	drop if ie==1 & surv5yr==0
	replace event=event+6
	gen nro=_n-6
	gen base=0
	gen base_se=0
	gen eff_ie=0
	gen eff_ie_se=0
	gen cov=0
	
	reg log_dspi_res ib5.event##i.ie i.vuosi i.vuosi#i.ie, vce(r)
		forvalues i = 1(1)16{
		qui{
			local c=`i'
			replace base=_b[`i'.event] in `c'
			replace base_se=_se[`i'.event] in `c'
			replace eff_ie=_b[`i'.event#1.ie] in `c'
			replace eff_ie_se=_se[`i'.event#1.ie] in `c'
			replace cov=e(V)["`i'.event", "`i'.event#1.ie"] in `c'
			}
			}
	
	keep nro-eff_ie_se cov
	rename eff_ie eff_ie_surv5yes
	rename eff_ie_se eff_ie_surv5yes_se
	keep if nro < 11
	
	* adding
	gen lvl_ie_surv5yes = base + eff_ie_surv5yes
	gen lvl_ie_surv5yes_se = base_se*base_se + eff_ie_surv5yes_se*eff_ie_surv5yes_se
	replace lvl_ie_surv5yes_se = sqrt(lvl_ie_surv5yes_se + 2*cov)
	keep nro lvl_ie_surv5yes lvl_ie_surv5yes_se
	
	merge 1:1 nro using "$results\04_incomemobility\fig4.dta", keepusing(base)
	drop _merge
	order lvl_ie_surv5yes lvl_ie_surv5yes_se, last
	label var lvl_ie_surv5yes "Survives 5 years"
	save "$results\04_incomemobility\figA8.dta", replace
restore

preserve
	drop if ie==1 & surv5yr==1
	replace event=event+6
	gen nro=_n-6
	gen base=0
	gen base_se=0
	gen eff_ie=0
	gen eff_ie_se=0
	gen cov=0
	
	reg log_dspi_res ib5.event##i.ie i.vuosi i.vuosi#i.ie, vce(r)
		forvalues i = 1(1)16{
		qui{
			local c=`i'
			replace base=_b[`i'.event] in `c'
			replace base_se=_se[`i'.event] in `c'
			replace eff_ie=_b[`i'.event#1.ie] in `c'
			replace eff_ie_se=_se[`i'.event#1.ie] in `c'
			replace cov=e(V)["`i'.event", "`i'.event#1.ie"] in `c'
			}
			}
	
	keep nro-eff_ie_se cov
	rename eff_ie eff_ie_surv5no
	rename eff_ie_se eff_ie_surv5no_se
	keep if nro < 11
	
	* adding
	gen lvl_ie_surv5no = base + eff_ie_surv5no
	gen lvl_ie_surv5no_se = base_se*base_se + eff_ie_surv5no_se*eff_ie_surv5no_se
	replace lvl_ie_surv5no_se = sqrt(lvl_ie_surv5no_se + 2*cov)
	keep nro lvl_ie_surv5no lvl_ie_surv5no_se
	
	merge 1:1 nro using "$results\04_incomemobility\figA8.dta"
	drop _merge
	order lvl_ie_surv5no lvl_ie_surv5no_se, last
	label var lvl_ie_surv5no "Does not survive 5 years"
	
	* Standard errors not needed --> drop the redundant variables
	drop *se
	save "$results\04_incomemobility\figA8.dta", replace
	
restore

/*
________________________________________________________________________________
Figure A9: Composition of market income sources by individual income
*/

/*
This exhibit relies on data from another project, where Juuti is a co-author:
Distributional National Accounts for Finland
*/

********************************************************************************

/*
Standard market income
*/

use if ptile_mkti>=90 using "W:\Inequality\Other\DINA\data\mktidare_0618.dta", clear
keep vuosi-mkti ib_owner ptile_mkti mkti_shr_l mkti_shr_e mkti_shr_c

* Get the top 0.1%
preserve
keep if ptile_mkti==100
forvalues i = 2006(1)2018{
	xtile ptile = mkti if vuosi==`i' /*& ika>=25 & ika<=65*/, nq(10)
	rename ptile ptile_`i'
}
gen mkti999=.
forvalues i = 2006(1)2018{
	replace mkti999=ptile_`i' if vuosi==`i' //& ika>=25 & ika<=65
}
keep if mkti999==10
keep vuosi shnro
gen top01=1
save "$results\04_incomemobility\p999temp.dta", replace
restore
merge 1:1 shnro vuosi using "$results\04_incomemobility\p999temp.dta"
drop if _merge==2
drop _merge
replace ptile_mkti=99.9 if ptile_mkti==100 & top01==.
erase "$results\04_incomemobility\p999temp.dta"

* Collapse the income sources by income bins
collapse mkti_shr_l mkti_shr_e mkti_shr_c, by(ptile_mkti)
gen mkti_shr_ec=mkti_shr_e+mkti_shr_c
rename ptile_mkti ptile

save "$results\04_incomemobility\figA9.dta", replace

********************************************************************************

/*
Market income w/ retained earnings and combine
*/

use if ptile_mkti_d_are_1>=90 using "W:\Inequality\Other\DINA\data\mktidare_0618.dta", replace
keep vuosi shnro mkti_d_are_1 ib_owner ptile_mkti_d_are_1 mkti_d_are_1_shr_l mkti_d_are_1_shr_e mkti_d_are_1_shr_c

* Get the top 0.1%
preserve
keep if ptile_mkti_d_are_1==100
forvalues i = 2006(1)2018{
	xtile ptile = mkti_d_are_1 if vuosi==`i' /*& ika>=25 & ika<=65*/, nq(10)
	rename ptile ptile_`i'
}
gen mkti_d_are_1999=.
forvalues i = 2006(1)2018{
	replace mkti_d_are_1999=ptile_`i' if vuosi==`i' //& ika>=25 & ika<=65
}
keep if mkti_d_are_1999==10
keep vuosi shnro
gen top01=1
save "$results\04_incomemobility\p999temp.dta", replace
restore
merge 1:1 shnro vuosi using "$results\04_incomemobility\p999temp.dta"
drop if _merge==2
drop _merge
replace ptile_mkti_d_are_1=99.9 if ptile_mkti_d_are_1==100 & top01==.
erase "$results\04_incomemobility\p999temp.dta"

* Collapse the income sources by income bins
collapse mkti_d_are_1_shr_l mkti_d_are_1_shr_e mkti_d_are_1_shr_c, by(ptile_mkti_d_are_1)
gen mkti_d_are_1_shr_ec=mkti_d_are_1_shr_e+mkti_d_are_1_shr_c
rename ptile_mkti_d_are_1 ptile
replace mkti_d_are_1_shr_ec = 1 - mkti_d_are_1_shr_l

* Combine
merge 1:1 ptile using "$results\04_incomemobility\figA9.dta"
keep ptile mkti_d_are_1_shr_l mkti_d_are_1_shr_ec mkti_shr_l mkti_shr_ec
order ptile mkti_shr_l mkti_shr_ec

save "$results\04_incomemobility\figA9.dta", replace


/*
________________________________________________________________________________
Figure B1: Average market income trajectories when accounting for retained earnings
*/

use "$data\01_main\folk_flown_merge_eventready.dta", clear

* residualize income with controls
rename sukup sex
rename ika_tmin1 age
gen agesq = age*age
gen agecub = agesq*age
rename highed_tmin1 edu
rename ptile_prnt parent
		gen edu_x_age=edu*age
		gen edu_x_agesq=edu*agesq
		gen edu_x_agecub=edu*agecub
		gen sex_x_age=sex*age
		gen sex_x_agesq=sex*agesq
		gen sex_x_agecub=sex*agecub
		gen edu_x_sex=edu*sex
foreach var of varlist log_dspi log_mkti log_mkti_adj1 {
reg `var' i.edu i.sex i.age i.edu_x_age i.edu_x_agesq i.edu_x_agecub i.sex_x_age i.sex_x_agesq i.sex_x_agecub i.edu_x_sex
predict res, res
sum `var', meanonly
replace res=res+r(mean)
rename res `var'_res
}

* define "treatment"
keep if event_ie!=. | event_we!=.
replace ie=0
replace ie=1 if event_ie!=. // IE labeled as IE already before becoming an entrepreneur
gen event=event_ie
replace event=event_we if event_ie==.

* Standard market income

preserve
	replace event=event+6
	gen nro=_n-6
	gen base=0
	gen base_se=0
	gen eff_ie=0
	gen eff_ie_se=0
	gen cov=0
	
	reg log_mkti_res ib5.event##i.ie i.vuosi i.vuosi#i.ie, vce(r)
		forvalues i = 1(1)16{
		qui{
			local c=`i'
			replace base=_b[`i'.event] in `c'
			replace base_se=_se[`i'.event] in `c'
			replace eff_ie=_b[`i'.event#1.ie] in `c'
			replace eff_ie_se=_se[`i'.event#1.ie] in `c'
			replace cov=e(V)["`i'.event", "`i'.event#1.ie"] in `c'
			}
			}
	
	keep nro-eff_ie_se cov
	keep if nro < 11
	
	* adding
	gen lvl_ie = base + eff_ie
	gen lvl_ie_se = base_se*base_se + eff_ie_se*eff_ie_se
	replace lvl_ie_se = sqrt(lvl_ie_se + 2*cov)
	order nro base base_se lvl_ie lvl_ie_se
	keep nro base base_se lvl_ie lvl_ie_se
	
	label var base "Market income, wage earners"
	label var lvl_ie "Market income, entrepreneurs"
	save "$results\04_incomemobility\figB1.dta", replace
restore

* Market income with retained earnings

preserve
	replace event=event+6
	gen nro=_n-6
	gen base=0
	gen base_se=0
	gen eff_ie=0
	gen eff_ie_se=0
	gen cov=0
	
	reg log_dspi_res ib5.event##i.ie i.vuosi i.vuosi#i.ie, vce(r)
		forvalues i = 1(1)16{
		qui{
			local c=`i'
			replace base=_b[`i'.event] in `c'
			replace base_se=_se[`i'.event] in `c'
			replace eff_ie=_b[`i'.event#1.ie] in `c'
			replace eff_ie_se=_se[`i'.event#1.ie] in `c'
			replace cov=e(V)["`i'.event", "`i'.event#1.ie"] in `c'
			}
			}
	
	keep nro-eff_ie_se cov
	keep if nro < 11
	
	* adding
	gen lvl_ie = base + eff_ie
	gen lvl_ie_se = base_se*base_se + eff_ie_se*eff_ie_se
	replace lvl_ie_se = sqrt(lvl_ie_se + 2*cov)
	order nro base base_se lvl_ie lvl_ie_se
	keep nro lvl_ie lvl_ie_se
	rename lvl_ie lvl_ie_re
	rename lvl_ie_se lvl_ie_re_se
	
	merge 1:1 nro using "$results\04_incomemobility\figB1.dta"
	drop _merge
	order lvl_ie_re lvl_ie_re_se, last

	label var lvl_ie_re "Market income with retained earnings, entrepreneurs"
	
	* Standard errors not needed --> drop the redundant variables
	drop *se
	save "$results\04_incomemobility\figB1.dta", replace
restore

/*
________________________________________________________________________________
Figure B2: Differences in income trajectories between entrepreneurs and wage earners by individual and parental income: Market income
*/

use "$data\01_main\folk_flown_merge_eventready.dta", clear
keep shnro id vuosi log_mkti event_ie-ika_tmin1 highed_tmin1 ptile_mkti_tmin1 ptile_prnt
rename sukup sex
rename ika_tmin1 age
gen agesq = age*age
gen agecub = agesq*age
rename highed_tmin1 edu

* Create interactions manually as 'xi: reghdfe' does not work with '#' or '##'
		gen edu_x_age=edu*age
		gen edu_x_agesq=edu*agesq
		gen edu_x_agecub=edu*agecub
		gen sex_x_age=sex*age
		gen sex_x_agesq=sex*agesq
		gen sex_x_agecub=sex*agecub
		gen edu_x_sex=edu*sex

* Quintiles		
egen quint_mkti_own = cut(ptile_mkti_tmin1), at(1(20)101)
egen quint_mkti_prnt = cut(ptile_prnt), at(1(20)101)

* Fails with IE interaction --> estimate IE and WE separately	
	
/*
								IE
*/

preserve


	keep if event_ie!=.

	egen long CL = group(quint_mkti_own quint_mkti_prnt) // 25 groups
	gen y=log_mkti
	rename event_ie event

		*
		gen event_time=_n-6		
		forval i=1/25 {
		gen b`i'=0
		gen se`i'=0
		}
		
		*char event[omit] -1
		gen eventt=event+6
		levelsof CL, local(list)
		levelsof eventt, local(eve)
		foreach c in `list'{
			foreach x in `eve'{
			gen interact_`c'_`x'=(CL==`c')*(eventt==`x')
			}
		}
		tab event, gen(eve)
		tab CL, gen(cl)
		forval i=1/25{
			drop interact_`i'_5
		}
		drop eve5 cl1
		drop interact_1_*

		reg y eve1-eve16 cl* interact_* i.vuosi i.edu i.sex i.age i.edu_x_age i.edu_x_agesq i.edu_x_agecub i.sex_x_age i.sex_x_agesq i.sex_x_agecub i.edu_x_sex, vce(r)
			replace b1=_b[_cons] if event_time==-1
			replace se1=_se[_cons] if event_time==-1
			forval i = 2/25 {
			replace b`i'=_b[_cons]+_b[cl`i'] if event_time==-1
			replace se`i'=_se[cl`i'] if event_time==-1
			}
		forval j = 1/4 {
			replace b1=_b[_cons]+_b[eve`j'] if _n==`j'
			replace se1=_se[eve`j'] if _n==`j'
			}
		forval j = 6/16 {
			replace b1=_b[_cons]+_b[eve`j'] if _n==`j'
			replace se1=_se[eve`j'] if _n==`j'
			}	
		forval j=1/4 {
		forval i = 2/25 {
			replace b`i'=_b[_cons]+_b[cl`i']+_b[eve`j']+_b[interact_`i'_`j'] if _n==`j'
			replace se`i'=_se[interact_`i'_`j'] if _n==`j'
			}
		}
		forval j=6/16 {
		forval i = 2/25 {
			replace b`i'=_b[_cons]+_b[cl`i']+_b[eve`j']+_b[interact_`i'_`j'] if _n==`j'
			replace se`i'=_se[interact_`i'_`j'] if _n==`j'
			}
		}

	keep if event_time<=10
	keep event_time - se25
	foreach var of varlist b1 - se25 {
		rename `var' `var'_ie
	}
	
	save "$results\04_incomemobility\ie.dta", replace

restore

/*
							WE
*/

preserve

	keep if event_we!=.

	egen long CL = group(quint_mkti_own quint_mkti_prnt) // 25 groups
	gen y=log_mkti
	rename event_we event
	* 50% sample only: uncomment if want to restrict sample size
	/*gen random=runiform() if event==-1
	gen keep=1 if random<=0.5
	bys id: egen min_keep=min(keep)
	keep if min_keep==1*/
	
		*
		gen event_time=_n-6		
		forval i=1/25 {
		gen b`i'=0
		gen se`i'=0
		}
		
		*char event[omit] -1
		gen eventt=event+6
		levelsof CL, local(list)
		levelsof eventt, local(eve)
		foreach c in `list'{
			foreach x in `eve'{
			gen interact_`c'_`x'=(CL==`c')*(eventt==`x')
			}
		}
		tab event, gen(eve)
		tab CL, gen(cl)
		forval i=1/25{
			drop interact_`i'_5
		}
		drop eve5 cl1 
		drop interact_1_*

		*di "$S_TIME $_DATE"
		*reghdfe y eventt##CL, vce(r) absorb(vuosi)
		*di "$S_TIME $_DATE"	
		di "$S_TIME $_DATE"
		reg y eve1-eve16 cl* interact_* i.vuosi i.edu i.sex i.age i.edu_x_age i.edu_x_agesq i.edu_x_agecub i.sex_x_age i.sex_x_agesq i.sex_x_agecub i.edu_x_sex, vce(r)
		di "$S_TIME $_DATE"
		
			replace b1=_b[_cons] if event_time==-1
			replace se1=_se[_cons] if event_time==-1
			forval i = 2/25 {
			replace b`i'=_b[_cons]+_b[cl`i'] if event_time==-1
			replace se`i'=_se[cl`i'] if event_time==-1
			}
		forval j = 1/4 {
			replace b1=_b[_cons]+_b[eve`j'] if _n==`j'
			replace se1=_se[eve`j'] if _n==`j'
			}
		forval j = 6/16 {
			replace b1=_b[_cons]+_b[eve`j'] if _n==`j'
			replace se1=_se[eve`j'] if _n==`j'
			}	
		forval j=1/4 {
		forval i = 2/25 {
			replace b`i'=_b[_cons]+_b[cl`i']+_b[eve`j']+_b[interact_`i'_`j'] if _n==`j'
			replace se`i'=_se[interact_`i'_`j'] if _n==`j'
			}
		}
		forval j=6/16 {
		forval i = 2/25 {
			replace b`i'=_b[_cons]+_b[cl`i']+_b[eve`j']+_b[interact_`i'_`j'] if _n==`j'
			replace se`i'=_se[interact_`i'_`j'] if _n==`j'
			}
		}

	keep if event_time<=10
	keep event_time - se25
	foreach var of varlist b1 - se25 {
		rename `var' `var'_we
	}
	
	save "$results\04_incomemobility\we.dta", replace

restore

/*
							Combine
*/

	use "$results\04_incomemobility\ie.dta", clear
	merge 1:1 event_time using "$results\04_incomemobility\we.dta"
	drop _merge
	rename event_time event
	save "$results\04_incomemobility\figB2.dta", replace

	erase "$results\04_incomemobility\ie.dta"
	erase "$results\04_incomemobility\we.dta"
/*
________________________________________________________________________________
Figure B3: Differences in income trajectories between entrepreneurs and wage earners by individual and parental income: Market income with retained earnings
*/

use "$data\01_main\folk_flown_merge_eventready.dta", clear
keep shnro id vuosi log_mkti_adj1 event_ie-ika_tmin1 highed_tmin1 ptile_mkti_adj1_tmin1 ptile_prnt
rename sukup sex
rename ika_tmin1 age
gen agesq = age*age
gen agecub = agesq*age
rename highed_tmin1 edu

* Create interactions manually as 'xi: reghdfe' does not work with '#' or '##'
		gen edu_x_age=edu*age
		gen edu_x_agesq=edu*agesq
		gen edu_x_agecub=edu*agecub
		gen sex_x_age=sex*age
		gen sex_x_agesq=sex*agesq
		gen sex_x_agecub=sex*agecub
		gen edu_x_sex=edu*sex

* Quintiles		
egen quint_mkti_adj1_own = cut(ptile_mkti_adj1_tmin1), at(1(20)101)
egen quint_mkti_adj1_prnt = cut(ptile_prnt), at(1(20)101)

* Fails with IE interaction --> estimate IE and WE separately	
	
/*
								IE
*/

preserve


	keep if event_ie!=.

	egen long CL = group(quint_mkti_adj1_own quint_mkti_adj1_prnt) // 25 groups
	gen y=log_mkti_adj1
	rename event_ie event

		*
		gen event_time=_n-6		
		forval i=1/25 {
		gen b`i'=0
		gen se`i'=0
		}
		
		*char event[omit] -1
		gen eventt=event+6
		levelsof CL, local(list)
		levelsof eventt, local(eve)
		foreach c in `list'{
			foreach x in `eve'{
			gen interact_`c'_`x'=(CL==`c')*(eventt==`x')
			}
		}
		tab event, gen(eve)
		tab CL, gen(cl)
		forval i=1/25{
			drop interact_`i'_5
		}
		drop eve5 cl1
		drop interact_1_*

		reg y eve1-eve16 cl* interact_* i.vuosi i.edu i.sex i.age i.edu_x_age i.edu_x_agesq i.edu_x_agecub i.sex_x_age i.sex_x_agesq i.sex_x_agecub i.edu_x_sex, vce(r)
			replace b1=_b[_cons] if event_time==-1
			replace se1=_se[_cons] if event_time==-1
			forval i = 2/25 {
			replace b`i'=_b[_cons]+_b[cl`i'] if event_time==-1
			replace se`i'=_se[cl`i'] if event_time==-1
			}
		forval j = 1/4 {
			replace b1=_b[_cons]+_b[eve`j'] if _n==`j'
			replace se1=_se[eve`j'] if _n==`j'
			}
		forval j = 6/16 {
			replace b1=_b[_cons]+_b[eve`j'] if _n==`j'
			replace se1=_se[eve`j'] if _n==`j'
			}	
		forval j=1/4 {
		forval i = 2/25 {
			replace b`i'=_b[_cons]+_b[cl`i']+_b[eve`j']+_b[interact_`i'_`j'] if _n==`j'
			replace se`i'=_se[interact_`i'_`j'] if _n==`j'
			}
		}
		forval j=6/16 {
		forval i = 2/25 {
			replace b`i'=_b[_cons]+_b[cl`i']+_b[eve`j']+_b[interact_`i'_`j'] if _n==`j'
			replace se`i'=_se[interact_`i'_`j'] if _n==`j'
			}
		}

	keep if event_time<=10
	keep event_time - se25
	foreach var of varlist b1 - se25 {
		rename `var' `var'_ie
	}
	
	save "$results\04_incomemobility\ie.dta", replace

restore

/*
							WE
*/

preserve

	keep if event_we!=.

	egen long CL = group(quint_mkti_adj1_own quint_mkti_adj1_prnt) // 25 groups
	gen y=log_mkti_adj1
	rename event_we event
	* 50% sample only: uncomment if want to restrict sample size
	/*gen random=runiform() if event==-1
	gen keep=1 if random<=0.5
	bys id: egen min_keep=min(keep)
	keep if min_keep==1*/
	
		*
		gen event_time=_n-6		
		forval i=1/25 {
		gen b`i'=0
		gen se`i'=0
		}
		
		*char event[omit] -1
		gen eventt=event+6
		levelsof CL, local(list)
		levelsof eventt, local(eve)
		foreach c in `list'{
			foreach x in `eve'{
			gen interact_`c'_`x'=(CL==`c')*(eventt==`x')
			}
		}
		tab event, gen(eve)
		tab CL, gen(cl)
		forval i=1/25{
			drop interact_`i'_5
		}
		drop eve5 cl1 
		drop interact_1_*

		*di "$S_TIME $_DATE"
		*reghdfe y eventt##CL, vce(r) absorb(vuosi)
		*di "$S_TIME $_DATE"	
		di "$S_TIME $_DATE"
		reg y eve1-eve16 cl* interact_* i.vuosi i.edu i.sex i.age i.edu_x_age i.edu_x_agesq i.edu_x_agecub i.sex_x_age i.sex_x_agesq i.sex_x_agecub i.edu_x_sex, vce(r)
		di "$S_TIME $_DATE"
		
			replace b1=_b[_cons] if event_time==-1
			replace se1=_se[_cons] if event_time==-1
			forval i = 2/25 {
			replace b`i'=_b[_cons]+_b[cl`i'] if event_time==-1
			replace se`i'=_se[cl`i'] if event_time==-1
			}
		forval j = 1/4 {
			replace b1=_b[_cons]+_b[eve`j'] if _n==`j'
			replace se1=_se[eve`j'] if _n==`j'
			}
		forval j = 6/16 {
			replace b1=_b[_cons]+_b[eve`j'] if _n==`j'
			replace se1=_se[eve`j'] if _n==`j'
			}	
		forval j=1/4 {
		forval i = 2/25 {
			replace b`i'=_b[_cons]+_b[cl`i']+_b[eve`j']+_b[interact_`i'_`j'] if _n==`j'
			replace se`i'=_se[interact_`i'_`j'] if _n==`j'
			}
		}
		forval j=6/16 {
		forval i = 2/25 {
			replace b`i'=_b[_cons]+_b[cl`i']+_b[eve`j']+_b[interact_`i'_`j'] if _n==`j'
			replace se`i'=_se[interact_`i'_`j'] if _n==`j'
			}
		}

	keep if event_time<=10
	keep event_time - se25
	foreach var of varlist b1 - se25 {
		rename `var' `var'_we
	}
	
	save "$results\04_incomemobility\we.dta", replace

restore

/*
							Combine
*/

	use "$results\04_incomemobility\ie.dta", clear
	merge 1:1 event_time using "$results\04_incomemobility\we.dta"
	drop _merge
	rename event_time event
	save "$results\04_incomemobility\figB3.dta", replace

	erase "$results\04_incomemobility\ie.dta"
	erase "$results\04_incomemobility\we.dta"

/*
________________________________________________________________________________
Table B1: Sensitivity of the gains from entrepreneurship to the estimation sample
*/

use "$data\01_main\folk_flown_merge_eventready.dta", clear
merge 1:1 shnro vuosi using "$data\01_main\folk_flown_merge_eventready_iespec.dta", keepusing(surv5yr)
drop _merge
bysort shnro: egen surv5yr_allyears = max(surv5yr)
drop surv5yr
rename surv5yr_allyears surv5yr

/*
Define gains and keep t-1
*/

xtset id vuosi, yearly
foreach var of varlist dspi mkti mkti_adj1 mkti_adj2 {
	gen gain_`var' = F11.log_`var' - log_`var'
	gen plus10_`var' = F11.`var'
}
keep if event_ie==-1
drop if gain_dspi==.

/*
Tabulate t-1 income, t+10 income, income gain in % and nobs
*/

gen nro=_n
gen sample=""
replace sample="Baseline" if nro==3
replace sample="Switchers (UIE-IE) dropped" if nro==5
replace sample="Parent entrepreneur: yes" if nro==7
replace sample="Parent entrepreneur: no" if nro==9
replace sample="Firm survives 5 years: yes" if nro==11
replace sample="Firm survives 5 years: no" if nro==13
replace sample="Starts 1998-2005 (dividend-based)" if nro==15
replace sample="Starts 2006-2014 (PRH-based)" if nro==17
replace sample="Starts 2006-2014 (dividend-based)" if nro==19

foreach var of varlist dspi mkti mkti_adj1 mkti_adj2 {
	gen min1_`var'_c=.
	gen plus10_`var'_c=.
	gen gain_`var'_c=.
	gen obs_`var'_c=.
}

**********
* Baseline

count
foreach var of varlist dspi mkti mkti_adj1 mkti_adj2 {
	replace obs_`var'_c=r(N) if nro==3
}
tabstat dspi plus10_dspi gain_dspi mkti plus10_mkti gain_mkti mkti_adj1 plus10_mkti_adj1 gain_mkti_adj1 mkti_adj2 plus10_mkti_adj2 gain_mkti_adj2, stat(mean) save
replace min1_dspi_c=r(StatTotal)[1,1] if nro==3
replace plus10_dspi_c=r(StatTotal)[1,2] if nro==3
replace gain_dspi_c=r(StatTotal)[1,3] if nro==3
replace min1_mkti_c=r(StatTotal)[1,4] if nro==3
replace plus10_mkti_c=r(StatTotal)[1,5] if nro==3
replace gain_mkti_c=r(StatTotal)[1,6] if nro==3
replace min1_mkti_adj1_c=r(StatTotal)[1,7] if nro==3
replace plus10_mkti_adj1_c=r(StatTotal)[1,8] if nro==3
replace gain_mkti_adj1_c=r(StatTotal)[1,9] if nro==3
replace min1_mkti_adj2_c=r(StatTotal)[1,10] if nro==3
replace plus10_mkti_adj2_c=r(StatTotal)[1,11] if nro==3
replace gain_mkti_adj2_c=r(StatTotal)[1,12] if nro==3
tabstat dspi plus10_dspi gain_dspi mkti plus10_mkti gain_mkti mkti_adj1 plus10_mkti_adj1 gain_mkti_adj1 mkti_adj2 plus10_mkti_adj2 gain_mkti_adj2, stat(semean) save
replace min1_dspi_c=r(StatTotal)[1,1] if nro==4
replace plus10_dspi_c=r(StatTotal)[1,2] if nro==4
replace gain_dspi_c=r(StatTotal)[1,3] if nro==4
replace min1_mkti_c=r(StatTotal)[1,4] if nro==4
replace plus10_mkti_c=r(StatTotal)[1,5] if nro==4
replace gain_mkti_c=r(StatTotal)[1,6] if nro==4
replace min1_mkti_adj1_c=r(StatTotal)[1,7] if nro==4
replace plus10_mkti_adj1_c=r(StatTotal)[1,8] if nro==4
replace gain_mkti_adj1_c=r(StatTotal)[1,9] if nro==4
replace min1_mkti_adj2_c=r(StatTotal)[1,10] if nro==4
replace plus10_mkti_adj2_c=r(StatTotal)[1,11] if nro==4
replace gain_mkti_adj2_c=r(StatTotal)[1,12] if nro==4

**********
* Switchers (UIE-IE) dropped

count if uie_to_ie==0
foreach var of varlist dspi mkti mkti_adj1 mkti_adj2 {
	replace obs_`var'_c=r(N) if nro==5
}
tabstat dspi plus10_dspi gain_dspi mkti plus10_mkti gain_mkti mkti_adj1 plus10_mkti_adj1 gain_mkti_adj1 mkti_adj2 plus10_mkti_adj2 gain_mkti_adj2 if uie_to_ie==0, stat(mean) save
replace min1_dspi_c=r(StatTotal)[1,1] if nro==5
replace plus10_dspi_c=r(StatTotal)[1,2] if nro==5
replace gain_dspi_c=r(StatTotal)[1,3] if nro==5
replace min1_mkti_c=r(StatTotal)[1,4] if nro==5
replace plus10_mkti_c=r(StatTotal)[1,5] if nro==5
replace gain_mkti_c=r(StatTotal)[1,6] if nro==5
replace min1_mkti_adj1_c=r(StatTotal)[1,7] if nro==5
replace plus10_mkti_adj1_c=r(StatTotal)[1,8] if nro==5
replace gain_mkti_adj1_c=r(StatTotal)[1,9] if nro==5
replace min1_mkti_adj2_c=r(StatTotal)[1,10] if nro==5
replace plus10_mkti_adj2_c=r(StatTotal)[1,11] if nro==5
replace gain_mkti_adj2_c=r(StatTotal)[1,12] if nro==5
tabstat dspi plus10_dspi gain_dspi mkti plus10_mkti gain_mkti mkti_adj1 plus10_mkti_adj1 gain_mkti_adj1 mkti_adj2 plus10_mkti_adj2 gain_mkti_adj2 if uie_to_ie==0, stat(semean) save
replace min1_dspi_c=r(StatTotal)[1,1] if nro==6
replace plus10_dspi_c=r(StatTotal)[1,2] if nro==6
replace gain_dspi_c=r(StatTotal)[1,3] if nro==6
replace min1_mkti_c=r(StatTotal)[1,4] if nro==6
replace plus10_mkti_c=r(StatTotal)[1,5] if nro==6
replace gain_mkti_c=r(StatTotal)[1,6] if nro==6
replace min1_mkti_adj1_c=r(StatTotal)[1,7] if nro==6
replace plus10_mkti_adj1_c=r(StatTotal)[1,8] if nro==6
replace gain_mkti_adj1_c=r(StatTotal)[1,9] if nro==6
replace min1_mkti_adj2_c=r(StatTotal)[1,10] if nro==6
replace plus10_mkti_adj2_c=r(StatTotal)[1,11] if nro==6
replace gain_mkti_adj2_c=r(StatTotal)[1,12] if nro==6

**********
* Parent entrepreneur: yes

count if prnt_entr==1
foreach var of varlist dspi mkti mkti_adj1 mkti_adj2 {
	replace obs_`var'_c=r(N) if nro==7
}
tabstat dspi plus10_dspi gain_dspi mkti plus10_mkti gain_mkti mkti_adj1 plus10_mkti_adj1 gain_mkti_adj1 mkti_adj2 plus10_mkti_adj2 gain_mkti_adj2 if prnt_entr==1, stat(mean) save
replace min1_dspi_c=r(StatTotal)[1,1] if nro==7
replace plus10_dspi_c=r(StatTotal)[1,2] if nro==7
replace gain_dspi_c=r(StatTotal)[1,3] if nro==7
replace min1_mkti_c=r(StatTotal)[1,4] if nro==7
replace plus10_mkti_c=r(StatTotal)[1,5] if nro==7
replace gain_mkti_c=r(StatTotal)[1,6] if nro==7
replace min1_mkti_adj1_c=r(StatTotal)[1,7] if nro==7
replace plus10_mkti_adj1_c=r(StatTotal)[1,8] if nro==7
replace gain_mkti_adj1_c=r(StatTotal)[1,9] if nro==7
replace min1_mkti_adj2_c=r(StatTotal)[1,10] if nro==7
replace plus10_mkti_adj2_c=r(StatTotal)[1,11] if nro==7
replace gain_mkti_adj2_c=r(StatTotal)[1,12] if nro==7
tabstat dspi plus10_dspi gain_dspi mkti plus10_mkti gain_mkti mkti_adj1 plus10_mkti_adj1 gain_mkti_adj1 mkti_adj2 plus10_mkti_adj2 gain_mkti_adj2 if prnt_entr==1, stat(semean) save
replace min1_dspi_c=r(StatTotal)[1,1] if nro==8
replace plus10_dspi_c=r(StatTotal)[1,2] if nro==8
replace gain_dspi_c=r(StatTotal)[1,3] if nro==8
replace min1_mkti_c=r(StatTotal)[1,4] if nro==8
replace plus10_mkti_c=r(StatTotal)[1,5] if nro==8
replace gain_mkti_c=r(StatTotal)[1,6] if nro==8
replace min1_mkti_adj1_c=r(StatTotal)[1,7] if nro==8
replace plus10_mkti_adj1_c=r(StatTotal)[1,8] if nro==8
replace gain_mkti_adj1_c=r(StatTotal)[1,9] if nro==8
replace min1_mkti_adj2_c=r(StatTotal)[1,10] if nro==8
replace plus10_mkti_adj2_c=r(StatTotal)[1,11] if nro==8
replace gain_mkti_adj2_c=r(StatTotal)[1,12] if nro==8

**********
* Parent entrepreneur: no

count if prnt_entr==0
foreach var of varlist dspi mkti mkti_adj1 mkti_adj2 {
	replace obs_`var'_c=r(N) if nro==9
}
tabstat dspi plus10_dspi gain_dspi mkti plus10_mkti gain_mkti mkti_adj1 plus10_mkti_adj1 gain_mkti_adj1 mkti_adj2 plus10_mkti_adj2 gain_mkti_adj2 if prnt_entr==0, stat(mean) save
replace min1_dspi_c=r(StatTotal)[1,1] if nro==9
replace plus10_dspi_c=r(StatTotal)[1,2] if nro==9
replace gain_dspi_c=r(StatTotal)[1,3] if nro==9
replace min1_mkti_c=r(StatTotal)[1,4] if nro==9
replace plus10_mkti_c=r(StatTotal)[1,5] if nro==9
replace gain_mkti_c=r(StatTotal)[1,6] if nro==9
replace min1_mkti_adj1_c=r(StatTotal)[1,7] if nro==9
replace plus10_mkti_adj1_c=r(StatTotal)[1,8] if nro==9
replace gain_mkti_adj1_c=r(StatTotal)[1,9] if nro==9
replace min1_mkti_adj2_c=r(StatTotal)[1,10] if nro==9
replace plus10_mkti_adj2_c=r(StatTotal)[1,11] if nro==9
replace gain_mkti_adj2_c=r(StatTotal)[1,12] if nro==9
tabstat dspi plus10_dspi gain_dspi mkti plus10_mkti gain_mkti mkti_adj1 plus10_mkti_adj1 gain_mkti_adj1 mkti_adj2 plus10_mkti_adj2 gain_mkti_adj2 if prnt_entr==0, stat(semean) save
replace min1_dspi_c=r(StatTotal)[1,1] if nro==10
replace plus10_dspi_c=r(StatTotal)[1,2] if nro==10
replace gain_dspi_c=r(StatTotal)[1,3] if nro==10
replace min1_mkti_c=r(StatTotal)[1,4] if nro==10
replace plus10_mkti_c=r(StatTotal)[1,5] if nro==10
replace gain_mkti_c=r(StatTotal)[1,6] if nro==10
replace min1_mkti_adj1_c=r(StatTotal)[1,7] if nro==10
replace plus10_mkti_adj1_c=r(StatTotal)[1,8] if nro==10
replace gain_mkti_adj1_c=r(StatTotal)[1,9] if nro==10
replace min1_mkti_adj2_c=r(StatTotal)[1,10] if nro==10
replace plus10_mkti_adj2_c=r(StatTotal)[1,11] if nro==10
replace gain_mkti_adj2_c=r(StatTotal)[1,12] if nro==10

**********
* Firm survives 5 years: yes

count if surv5yr==1
foreach var of varlist dspi mkti mkti_adj1 mkti_adj2 {
	replace obs_`var'_c=r(N) if nro==11
}
tabstat dspi plus10_dspi gain_dspi mkti plus10_mkti gain_mkti mkti_adj1 plus10_mkti_adj1 gain_mkti_adj1 mkti_adj2 plus10_mkti_adj2 gain_mkti_adj2 if surv5yr==1, stat(mean) save
replace min1_dspi_c=r(StatTotal)[1,1] if nro==11
replace plus10_dspi_c=r(StatTotal)[1,2] if nro==11
replace gain_dspi_c=r(StatTotal)[1,3] if nro==11
replace min1_mkti_c=r(StatTotal)[1,4] if nro==11
replace plus10_mkti_c=r(StatTotal)[1,5] if nro==11
replace gain_mkti_c=r(StatTotal)[1,6] if nro==11
replace min1_mkti_adj1_c=r(StatTotal)[1,7] if nro==11
replace plus10_mkti_adj1_c=r(StatTotal)[1,8] if nro==11
replace gain_mkti_adj1_c=r(StatTotal)[1,9] if nro==11
replace min1_mkti_adj2_c=r(StatTotal)[1,10] if nro==11
replace plus10_mkti_adj2_c=r(StatTotal)[1,11] if nro==11
replace gain_mkti_adj2_c=r(StatTotal)[1,12] if nro==11
tabstat dspi plus10_dspi gain_dspi mkti plus10_mkti gain_mkti mkti_adj1 plus10_mkti_adj1 gain_mkti_adj1 mkti_adj2 plus10_mkti_adj2 gain_mkti_adj2 if surv5yr==1, stat(semean) save
replace min1_dspi_c=r(StatTotal)[1,1] if nro==12
replace plus10_dspi_c=r(StatTotal)[1,2] if nro==12
replace gain_dspi_c=r(StatTotal)[1,3] if nro==12
replace min1_mkti_c=r(StatTotal)[1,4] if nro==12
replace plus10_mkti_c=r(StatTotal)[1,5] if nro==12
replace gain_mkti_c=r(StatTotal)[1,6] if nro==12
replace min1_mkti_adj1_c=r(StatTotal)[1,7] if nro==12
replace plus10_mkti_adj1_c=r(StatTotal)[1,8] if nro==12
replace gain_mkti_adj1_c=r(StatTotal)[1,9] if nro==12
replace min1_mkti_adj2_c=r(StatTotal)[1,10] if nro==12
replace plus10_mkti_adj2_c=r(StatTotal)[1,11] if nro==12
replace gain_mkti_adj2_c=r(StatTotal)[1,12] if nro==12

**********
* Firm survives 5 years: no

count if surv5yr==0
foreach var of varlist dspi mkti mkti_adj1 mkti_adj2 {
	replace obs_`var'_c=r(N) if nro==13
}
tabstat dspi plus10_dspi gain_dspi mkti plus10_mkti gain_mkti mkti_adj1 plus10_mkti_adj1 gain_mkti_adj1 mkti_adj2 plus10_mkti_adj2 gain_mkti_adj2 if surv5yr==0, stat(mean) save
replace min1_dspi_c=r(StatTotal)[1,1] if nro==13
replace plus10_dspi_c=r(StatTotal)[1,2] if nro==13
replace gain_dspi_c=r(StatTotal)[1,3] if nro==13
replace min1_mkti_c=r(StatTotal)[1,4] if nro==13
replace plus10_mkti_c=r(StatTotal)[1,5] if nro==13
replace gain_mkti_c=r(StatTotal)[1,6] if nro==13
replace min1_mkti_adj1_c=r(StatTotal)[1,7] if nro==13
replace plus10_mkti_adj1_c=r(StatTotal)[1,8] if nro==13
replace gain_mkti_adj1_c=r(StatTotal)[1,9] if nro==13
replace min1_mkti_adj2_c=r(StatTotal)[1,10] if nro==13
replace plus10_mkti_adj2_c=r(StatTotal)[1,11] if nro==13
replace gain_mkti_adj2_c=r(StatTotal)[1,12] if nro==13
tabstat dspi plus10_dspi gain_dspi mkti plus10_mkti gain_mkti mkti_adj1 plus10_mkti_adj1 gain_mkti_adj1 mkti_adj2 plus10_mkti_adj2 gain_mkti_adj2 if surv5yr==0, stat(semean) save
replace min1_dspi_c=r(StatTotal)[1,1] if nro==14
replace plus10_dspi_c=r(StatTotal)[1,2] if nro==14
replace gain_dspi_c=r(StatTotal)[1,3] if nro==14
replace min1_mkti_c=r(StatTotal)[1,4] if nro==14
replace plus10_mkti_c=r(StatTotal)[1,5] if nro==14
replace gain_mkti_c=r(StatTotal)[1,6] if nro==14
replace min1_mkti_adj1_c=r(StatTotal)[1,7] if nro==14
replace plus10_mkti_adj1_c=r(StatTotal)[1,8] if nro==14
replace gain_mkti_adj1_c=r(StatTotal)[1,9] if nro==14
replace min1_mkti_adj2_c=r(StatTotal)[1,10] if nro==14
replace plus10_mkti_adj2_c=r(StatTotal)[1,11] if nro==14
replace gain_mkti_adj2_c=r(StatTotal)[1,12] if nro==14

**********
* Starts 1998-2005 (dividend-based)

count if vuosi<2006
foreach var of varlist dspi mkti mkti_adj1 mkti_adj2 {
	replace obs_`var'_c=r(N) if nro==15
}
tabstat dspi plus10_dspi gain_dspi mkti plus10_mkti gain_mkti mkti_adj1 plus10_mkti_adj1 gain_mkti_adj1 mkti_adj2 plus10_mkti_adj2 gain_mkti_adj2 if vuosi<2006, stat(mean) save
replace min1_dspi_c=r(StatTotal)[1,1] if nro==15
replace plus10_dspi_c=r(StatTotal)[1,2] if nro==15
replace gain_dspi_c=r(StatTotal)[1,3] if nro==15
replace min1_mkti_c=r(StatTotal)[1,4] if nro==15
replace plus10_mkti_c=r(StatTotal)[1,5] if nro==15
replace gain_mkti_c=r(StatTotal)[1,6] if nro==15
replace min1_mkti_adj1_c=r(StatTotal)[1,7] if nro==15
replace plus10_mkti_adj1_c=r(StatTotal)[1,8] if nro==15
replace gain_mkti_adj1_c=r(StatTotal)[1,9] if nro==15
replace min1_mkti_adj2_c=r(StatTotal)[1,10] if nro==15
replace plus10_mkti_adj2_c=r(StatTotal)[1,11] if nro==15
replace gain_mkti_adj2_c=r(StatTotal)[1,12] if nro==15
tabstat dspi plus10_dspi gain_dspi mkti plus10_mkti gain_mkti mkti_adj1 plus10_mkti_adj1 gain_mkti_adj1 mkti_adj2 plus10_mkti_adj2 gain_mkti_adj2 if vuosi<2006, stat(semean) save
replace min1_dspi_c=r(StatTotal)[1,1] if nro==16
replace plus10_dspi_c=r(StatTotal)[1,2] if nro==16
replace gain_dspi_c=r(StatTotal)[1,3] if nro==16
replace min1_mkti_c=r(StatTotal)[1,4] if nro==16
replace plus10_mkti_c=r(StatTotal)[1,5] if nro==16
replace gain_mkti_c=r(StatTotal)[1,6] if nro==16
replace min1_mkti_adj1_c=r(StatTotal)[1,7] if nro==16
replace plus10_mkti_adj1_c=r(StatTotal)[1,8] if nro==16
replace gain_mkti_adj1_c=r(StatTotal)[1,9] if nro==16
replace min1_mkti_adj2_c=r(StatTotal)[1,10] if nro==16
replace plus10_mkti_adj2_c=r(StatTotal)[1,11] if nro==16
replace gain_mkti_adj2_c=r(StatTotal)[1,12] if nro==16

**********
* Starts 2006-2014 (PRH-based)

count if vuosi>=2006
foreach var of varlist dspi mkti mkti_adj1 mkti_adj2 {
	replace obs_`var'_c=r(N) if nro==17
}
tabstat dspi plus10_dspi gain_dspi mkti plus10_mkti gain_mkti mkti_adj1 plus10_mkti_adj1 gain_mkti_adj1 mkti_adj2 plus10_mkti_adj2 gain_mkti_adj2 if vuosi>=2006, stat(mean) save
replace min1_dspi_c=r(StatTotal)[1,1] if nro==17
replace plus10_dspi_c=r(StatTotal)[1,2] if nro==17
replace gain_dspi_c=r(StatTotal)[1,3] if nro==17
replace min1_mkti_c=r(StatTotal)[1,4] if nro==17
replace plus10_mkti_c=r(StatTotal)[1,5] if nro==17
replace gain_mkti_c=r(StatTotal)[1,6] if nro==17
replace min1_mkti_adj1_c=r(StatTotal)[1,7] if nro==17
replace plus10_mkti_adj1_c=r(StatTotal)[1,8] if nro==17
replace gain_mkti_adj1_c=r(StatTotal)[1,9] if nro==17
replace min1_mkti_adj2_c=r(StatTotal)[1,10] if nro==17
replace plus10_mkti_adj2_c=r(StatTotal)[1,11] if nro==17
replace gain_mkti_adj2_c=r(StatTotal)[1,12] if nro==17
tabstat dspi plus10_dspi gain_dspi mkti plus10_mkti gain_mkti mkti_adj1 plus10_mkti_adj1 gain_mkti_adj1 mkti_adj2 plus10_mkti_adj2 gain_mkti_adj2 if vuosi>=2006, stat(semean) save
replace min1_dspi_c=r(StatTotal)[1,1] if nro==18
replace plus10_dspi_c=r(StatTotal)[1,2] if nro==18
replace gain_dspi_c=r(StatTotal)[1,3] if nro==18
replace min1_mkti_c=r(StatTotal)[1,4] if nro==18
replace plus10_mkti_c=r(StatTotal)[1,5] if nro==18
replace gain_mkti_c=r(StatTotal)[1,6] if nro==18
replace min1_mkti_adj1_c=r(StatTotal)[1,7] if nro==18
replace plus10_mkti_adj1_c=r(StatTotal)[1,8] if nro==18
replace gain_mkti_adj1_c=r(StatTotal)[1,9] if nro==18
replace min1_mkti_adj2_c=r(StatTotal)[1,10] if nro==18
replace plus10_mkti_adj2_c=r(StatTotal)[1,11] if nro==18
replace gain_mkti_adj2_c=r(StatTotal)[1,12] if nro==18

**********
* Starts 2006-2014 (dividend-based)

rename event_ie event_ie_orig
merge 1:1 shnro vuosi using "$data\01_main\folk_flown_merge_taxadmin0614.dta"
drop if _merge==2
drop _merge

count if vuosi>=2006 & event_ie==-1
foreach var of varlist dspi mkti mkti_adj1 mkti_adj2 {
	replace obs_`var'_c=r(N) if nro==19
}
tabstat dspi plus10_dspi gain_dspi mkti plus10_mkti gain_mkti mkti_adj1 plus10_mkti_adj1 gain_mkti_adj1 mkti_adj2 plus10_mkti_adj2 gain_mkti_adj2 if vuosi>=2006 & event_ie==-1, stat(mean) save
replace min1_dspi_c=r(StatTotal)[1,1] if nro==19
replace plus10_dspi_c=r(StatTotal)[1,2] if nro==19
replace gain_dspi_c=r(StatTotal)[1,3] if nro==19
replace min1_mkti_c=r(StatTotal)[1,4] if nro==19
replace plus10_mkti_c=r(StatTotal)[1,5] if nro==19
replace gain_mkti_c=r(StatTotal)[1,6] if nro==19
replace min1_mkti_adj1_c=r(StatTotal)[1,7] if nro==19
replace plus10_mkti_adj1_c=r(StatTotal)[1,8] if nro==19
replace gain_mkti_adj1_c=r(StatTotal)[1,9] if nro==19
replace min1_mkti_adj2_c=r(StatTotal)[1,10] if nro==19
replace plus10_mkti_adj2_c=r(StatTotal)[1,11] if nro==19
replace gain_mkti_adj2_c=r(StatTotal)[1,12] if nro==19
tabstat dspi plus10_dspi gain_dspi mkti plus10_mkti gain_mkti mkti_adj1 plus10_mkti_adj1 gain_mkti_adj1 mkti_adj2 plus10_mkti_adj2 gain_mkti_adj2 if vuosi>=2006 & event_ie==-1, stat(semean) save
replace min1_dspi_c=r(StatTotal)[1,1] if nro==20
replace plus10_dspi_c=r(StatTotal)[1,2] if nro==20
replace gain_dspi_c=r(StatTotal)[1,3] if nro==20
replace min1_mkti_c=r(StatTotal)[1,4] if nro==20
replace plus10_mkti_c=r(StatTotal)[1,5] if nro==20
replace gain_mkti_c=r(StatTotal)[1,6] if nro==20
replace min1_mkti_adj1_c=r(StatTotal)[1,7] if nro==20
replace plus10_mkti_adj1_c=r(StatTotal)[1,8] if nro==20
replace gain_mkti_adj1_c=r(StatTotal)[1,9] if nro==20
replace min1_mkti_adj2_c=r(StatTotal)[1,10] if nro==20
replace plus10_mkti_adj2_c=r(StatTotal)[1,11] if nro==20
replace gain_mkti_adj2_c=r(StatTotal)[1,12] if nro==20

/*
Facelift
*/

keep if nro<=20
replace sample="(SE of mean)" if sample=="" & nro>=4
keep sample-obs_mkti_adj2_c
save "$results\04_incomemobility\tabB1.dta", replace

/*
________________________________________________________________________________
Figure B4: Disposable income trajectories using a balanced panel of individuals
*/

use "$data\01_main\folk_flown_merge_eventready.dta", clear

* residualize income with controls
rename sukup sex
rename ika_tmin1 age
gen agesq = age*age
gen agecub = agesq*age
rename highed_tmin1 edu
rename ptile_prnt parent
		gen edu_x_age=edu*age
		gen edu_x_agesq=edu*agesq
		gen edu_x_agecub=edu*agecub
		gen sex_x_age=sex*age
		gen sex_x_agesq=sex*agesq
		gen sex_x_agecub=sex*agecub
		gen edu_x_sex=edu*sex
foreach var of varlist log_dspi log_mkti log_mkti_adj1 {
reg `var' i.edu i.sex i.age i.edu_x_age i.edu_x_agesq i.edu_x_agecub i.sex_x_age i.sex_x_agesq i.sex_x_agecub i.edu_x_sex
predict res, res
sum `var', meanonly
replace res=res+r(mean)
rename res `var'_res
}

* define "treatment"
keep if event_ie!=. | event_we!=.
replace ie=0
replace ie=1 if event_ie!=. // IE labeled as IE already before becoming an entrepreneur
gen event=event_ie
replace event=event_we if event_ie==.

* define the balanced samples
preserve
keep if inrange(event,-1,10)
bysort shnro: gen nobs = _N
keep if nobs==12
bysort shnro: gen nr = _n
keep if nr==1
drop nr
keep shnro
gen bal_m1p10=1
save "$data\99_temp\bal_m1p10.dta", replace
restore
preserve
keep if inrange(event,-5,10)
bysort shnro: gen nobs = _N
keep if nobs==16
bysort shnro: gen nr = _n
keep if nr==1
drop nr
keep shnro
gen bal_ful=1
save "$data\99_temp\bal_ful.dta", replace
restore

merge m:1 shnro using "$data\99_temp\bal_m1p10.dta"
drop _merge
merge m:1 shnro using "$data\99_temp\bal_ful.dta"
drop _merge

erase "$data\99_temp\bal_m1p10.dta"
erase "$data\99_temp\bal_ful.dta"

preserve
	keep if bal_m1p10==1
	replace event=event+6
	gen nro=_n-6
	gen base=0
	gen base_se=0
	gen eff_ie=0
	gen eff_ie_se=0
	gen cov=0
	
	reg log_dspi_res ib5.event##i.ie i.vuosi i.vuosi#i.ie, vce(r)
		forvalues i = 1(1)16{
		qui{
			local c=`i'
			replace base=_b[`i'.event] in `c'
			replace base_se=_se[`i'.event] in `c'
			replace eff_ie=_b[`i'.event#1.ie] in `c'
			replace eff_ie_se=_se[`i'.event#1.ie] in `c'
			replace cov=e(V)["`i'.event", "`i'.event#1.ie"] in `c'
			}
			}
	
	keep nro-eff_ie_se cov
	keep if nro < 11
	
	* adding
	gen lvl_ie = base + eff_ie
	gen lvl_ie_se = base_se*base_se + eff_ie_se*eff_ie_se
	replace lvl_ie_se = sqrt(lvl_ie_se + 2*cov)
	keep nro base base_se lvl_ie lvl_ie_se
	
	rename base base_bal_m1p10
	rename base_se base_bal_m1p10_se
	rename lvl_ie lvl_ie_bal_m1p10
	rename lvl_ie_se lvl_ie_bal_m1p10_se
	
	save "$results\04_incomemobility\figB4.dta", replace
restore

preserve
	keep if bal_ful==1
	replace event=event+6
	gen nro=_n-6
	gen base=0
	gen base_se=0
	gen eff_ie=0
	gen eff_ie_se=0
	gen cov=0
	
	reg log_dspi_res ib5.event##i.ie i.vuosi i.vuosi#i.ie, vce(r)
		forvalues i = 1(1)16{
		qui{
			local c=`i'
			replace base=_b[`i'.event] in `c'
			replace base_se=_se[`i'.event] in `c'
			replace eff_ie=_b[`i'.event#1.ie] in `c'
			replace eff_ie_se=_se[`i'.event#1.ie] in `c'
			replace cov=e(V)["`i'.event", "`i'.event#1.ie"] in `c'
			}
			}
	
	keep nro-eff_ie_se cov
	keep if nro < 11
	
	* adding
	gen lvl_ie = base + eff_ie
	gen lvl_ie_se = base_se*base_se + eff_ie_se*eff_ie_se
	replace lvl_ie_se = sqrt(lvl_ie_se + 2*cov)
	keep nro base base_se lvl_ie lvl_ie_se
	
	rename base base_bal_ful
	rename base_se base_bal_ful_se
	rename lvl_ie lvl_ie_bal_ful
	rename lvl_ie_se lvl_ie_bal_ful_se
	
	merge 1:1 nro using "$results\04_incomemobility\figB4.dta"
	drop _merge
	order base_bal_ful base_bal_ful_se lvl_ie_bal_ful lvl_ie_bal_ful_se, last

	* Standard errors not needed --> drop the redundant variables
	drop *se
	save "$results\04_incomemobility\figB4.dta", replace
restore


/*
________________________________________________________________________________
Figure B5: Gains in disposable income by industry of the new incorporated business
*/

use "$data\01_main\folk_flown_merge_eventready.dta", clear

rename sukup sex
rename ika_tmin1 age
gen agesq = age*age
gen agecub = agesq*age
rename highed_tmin1 edu
		gen edu_x_age=edu*age
		gen edu_x_agesq=edu*agesq
		gen edu_x_agecub=edu*agecub
		gen sex_x_age=sex*age
		gen sex_x_agesq=sex*agesq
		gen sex_x_agecub=sex*agecub
		gen edu_x_sex=edu*sex

* keep only those we observe both at t-1 and t+10
keep if event_ie==-1 | event_ie==10
bysort shnro: gen nobs = _N
keep if nobs==2

xtset id vuosi, yearly
gen gain = F11.log_dspi - log_dspi
gen tolasentrep=F11.tol2dig
keep if event_ie==-1
drop if gain==.

cd "$results\04_incomemobility"
qui reg gain i.tolasentrep, vce(r)
outreg2 using figB5, stats(coef se) dec(4) dta replace
qui reg gain i.tolasentrep i.edu i.sex i.age i.edu_x_age i.edu_x_agesq i.edu_x_agecub i.sex_x_age i.sex_x_agesq i.sex_x_agecub i.edu_x_sex, vce(r)
outreg2 using figB5, stats(coef se) dec(4) dta append
erase figB5.txt

use figB5_dta.dta, clear
drop if _n>173
drop if _n<4
rename v1 ind
rename v2 raw
rename v3 ctrl

* Modify to be able to plot
local models raw ctrl
foreach var of local models {
	
	matrix `var' = J(85,3,0)
	local j = 1
	local i = 1
	while `i' < 170 {
		
		local estim = ind[`i']
		local rownames_`var' `rownames_`var'' `estim'
		local significant = strpos(`var'[`i'], "*") > 0
		local mean = subinstr(`var'[`i'],"*","",.)
		local se = subinstr(`var'[`i' + 1],"(","",.)
		local se = subinstr("`se'",")","",.)
		
		local mean_num: di `mean'
		local se_num: di `se'
		matrix `var'[`j',1] = `mean_num'
		matrix `var'[`j',2] = `se_num'
		matrix `var'[`j',3] = `significant'
		local i = `i' + 2
		local ++j
		
	}
	
	matrix rownames `var' = `rownames_`var''
	matrix colnames `var' = b se star
}

matlist raw
svmat raw, names(model_raw)
matlist ctrl
svmat ctrl, names(model_ctrl)

drop raw ctrl model_raw3 model_ctrl3
rename model_raw1 raw
rename model_raw2 raw_se
rename model_ctrl1 ctrl
rename model_ctrl2 ctrl_se
preserve
keep ind
drop if ind==""
replace ind = subinstr(ind,".tolasentrep", "", .)
gen nr=_n
save figB5_dta.dta, replace
restore
drop if _n>85
drop ind
gen nr=_n
merge 1:1 nr using figB5_dta
drop _merge nr

* make eveything proportional to the worst industry (32)
sum raw
replace raw = raw - r(min)
sum ctrl
replace ctrl = ctrl - r(min)

destring ind, replace
keep if ind==2 | ///
		ind==8 | ///
		ind==10 | ///
		ind==13 | ///
		ind==16 | ///
		ind==18 | ///
		ind==20 | ///
		ind==22 | ///
		ind==23 | ///
		ind==25 | ///
		ind==26 | ///
		ind==27 | ///
		ind==28 | ///
		ind==29 | ///
		ind==30 | ///
		ind==31 | ///
		ind==33 | ///
		ind==35 | ///
		ind==38 | ///
		ind==41 | ///
		ind==42 | ///
		ind==43 | ///
		ind==45 | ///
		ind==46 | ///
		ind==47 | ///
		ind==49 | ///
		ind==50 | ///
		ind==52 | ///
		ind==55 | ///
		ind==56 | ///
		ind==58 | ///
		ind==59 | ///
		ind==60 | ///
		ind==61 | ///
		ind==62 | ///
		ind==63 | ///
		ind==64 | ///
		ind==66 | ///
		ind==68 | ///
		ind==69 | ///
		ind==70 | ///
		ind==71 | ///
		ind==72 | ///
		ind==73 | ///
		ind==74 | ///
		ind==75 | ///
		ind==77 | ///
		ind==78 | ///
		ind==79 | ///
		ind==80 | ///
		ind==81 | ///
		ind==82 | ///
		ind==85 | ///
		ind==86 | ///
		ind==87 | ///
		ind==88 | ///
		ind==90 | ///
		ind==93 | ///
		ind==95 | ///
		ind==96
		
save "$results\04_incomemobility\figB5.dta", replace
erase "$results\04_incomemobility\figB5_dta.dta"